package com.example.notering;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.example.adapter.DiaryAdapter;
import com.example.dao.Diary;
import com.example.data.CacheDiary;

public class DiaryFragment extends Fragment{
	Button addButton;  
	DiaryAdapter diaryAdapter;//适配器
	private List<Diary> diaryContentList = new ArrayList<Diary>();// 内容列表
	private ListView diaryList;//展示list
	

    @Override  
    public View onCreateView(LayoutInflater inflater, ViewGroup container,  
            Bundle savedInstanceState) { 
    	
    	//从rootview找到控件
        View rootView = inflater.inflate(R.layout.activity_diary, container, false);
        addButton = (Button)rootView.findViewById(R.id.add_tips);
        diaryList = (ListView)rootView.findViewById(R.id.diary_list);
        
        setProperty();
        
        setData();
        
        setListener();
        
        return rootView;  
    }  
    

	private void setData() {
		//数据库数据取出
        diaryContentList.addAll(CacheDiary.getInstance(this.getActivity()).loadAllDiary());
        //设置数据源
        diaryAdapter = new DiaryAdapter(this.getActivity(), diaryContentList);
        diaryList.setAdapter(diaryAdapter);
	}


	private void setProperty() {
		addButton.setAlpha(100);
        diaryList.setOnCreateContextMenuListener(this);
	}


	private void setListener() {
		//选项点击事件
		diaryList.setOnItemClickListener(new OnItemClickListener() {
			
			@Override  
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            	Intent intent = new Intent(DiaryFragment.this.getActivity(),ViewActivity.class);
            	Bundle bundle = new Bundle();
            	bundle.putParcelable("item", diaryContentList.get(position));
            	intent.putExtra("bundle", bundle);
            	startActivity(intent);
            }});
        
        //设置添加日记监听事件  
        addButton.setOnClickListener(new View.OnClickListener() {  
              
            @Override  
            public void onClick(View v) {  
            	Intent intent = new Intent(DiaryFragment.this.getActivity(),AddDiaryActivity.class);
            	startActivity(intent);
            }});
	}


	@Override
    public void onCreateContextMenu(ContextMenu menu, View view, 
            ContextMenuInfo menuInfo) {
        menu.setHeaderTitle("删除？");
        //添加菜单项
        menu.add(0, Menu.FIRST, 0, "是");
        menu.add(0, Menu.FIRST+1, 0, "否");
    }
    //菜单单击响应
    @Override
    public boolean onContextItemSelected(MenuItem item){
    	if (item.getMenuInfo() instanceof AdapterContextMenuInfo) {  
            AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();  
                //处理菜单的点击事件  
            switch (item.getItemId()) {  
            case Menu.FIRST:  
            	CacheDiary.getInstance(this.getActivity()).deleteDiary(diaryContentList.get(menuInfo.position));
            	update();
                break;  
            case Menu.FIRST+1:
                break;
            }
        }  
        return super.onContextItemSelected(item);
    }
    
    @Override  
    public void onStart() {
    	super.onStart();
    	update();
    }  
    
    public void update(){
    	diaryContentList.clear();
    	diaryContentList.addAll(CacheDiary.getInstance(this.getActivity()).loadAllDiary());
    	diaryAdapter.notifyDataSetChanged();
    }
}
